/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.integral.mq;

import cn.hutool.core.util.ObjectUtil;
import com.medusa.gruul.integral.api.constant.IntegralQueueNameConstant;
import com.medusa.gruul.integral.api.model.IntegralOrderDTO;
import com.medusa.gruul.integral.service.IIntegralDetailService;
import com.medusa.gruul.integral.service.IIntegralOrderService;
import com.medusa.gruul.integral.service.IIntegralShopService;
import com.medusa.gruul.order.api.model.OrderVo;
import com.medusa.gruul.payment.api.model.dto.WxPayNotifyResultDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Description: 积分队列监听
 * @author xiaoq
 * Date : 2019/12/19 11:16
 */
@Slf4j
@Component
public class IntegralListener {


	@Autowired
	private IIntegralDetailService integralDetailService;

	@Autowired
	private IIntegralShopService integralShopService;

    @Autowired
    private IIntegralOrderService integralOrderService;

	//===============================订阅======================================//

	/**
	 * 订单完成创建积分明细及通知用户加积分
	 * @param orderVo 订单详情
	 */
	@RabbitListener(queues = IntegralQueueNameConstant.ORDER_PAY_OK)
	public void receiveSendPayedOrderMessage(OrderVo orderVo) {
		log.info("receive message:" + orderVo.toString());
		if (ObjectUtil.isNull(orderVo)) {
			//手动确认
			return;
		}
		try {
            integralDetailService.orderFinish(orderVo);
        }catch (Exception ex){
		    log.error("用户增加积分消费失败",ex);
        }
	}

    /**
     * 积分订单支付成功
     * @param wxPayNotifyResultDto 支付成功回调参数
     */
    @RabbitListener(queues = IntegralQueueNameConstant.INTEGRAL_PAY)
    public  void  integralPayOkMessage(WxPayNotifyResultDto wxPayNotifyResultDto){
        try {
            integralOrderService.integralOrderDone(Long.valueOf(wxPayNotifyResultDto.getOutTradeNo()));
        }catch (Exception ex){
            log.error("积分订单支付成功回调失败",ex);
        }
    }
    /**
     * 积分订单超时未支付
     */
    @RabbitListener(queues = IntegralQueueNameConstant.INTEGRAL_PAY_FAIL)
    public void integralPayFail(IntegralOrderDTO integralOrder){
        if (integralOrder == null || integralOrder.getOrderId()==null){
            return;
        }
        try{
            integralOrderService. delIntegralOrder( integralOrder.getOrderId());
        }catch (Exception e){
            log.error("err:",e);
        }
    }


	/**
	 * 取消订单失败  扣除归还积分 -1
	 * @param orderFailMessage
	 * @param properties
	 * @param channel
	 */
	//    @RabbitListener(queues = IntegralQueueNameConstant.CancelOrderFail)
	//	public void receiveCancelFailOrderMessage(OrderFailMessage orderFailMessage, MessageProperties properties, Channel channel){
	//        log.info("receive message:" + orderFailMessage.toString());
	//    }

	/**
	 * 订单创建失败  归还扣除积分 +1
	 * @param orderFailMessage
	 * @param properties
	 * @param channel
	 */
	//    @RabbitListener(queues = IntegralQueueNameConstant.CreateOrderFail)
	//	public void receiveCreateOrderFailMessage(OrderFailMessage orderFailMessage, MessageProperties properties, Channel channel){
	//        log.info("receive message:" + orderFailMessage.toString());
	//        integralDetailService.createOrderFail(orderFailMessage);
	//    }


}
